package com.cosc716.restaurant.system;

import java.util.ArrayList;
import com.cosc716.restaurant.commands.Command;

public class Invoker {
	
	// private reference to Aggregator singleton
	private Aggregator aggregator;
	
	// private ivar holds the singleton reference
	private static Invoker invoker;
	
	// private queue of commands; eg, these can be batched
	private ArrayList<Command> commands;

	// prevent clients from using constructor
	private Invoker() {
		if (null == aggregator) {
			aggregator = Aggregator.getInstance();
		}
		
		commands = new ArrayList<Command>();
	}
	
	/**
	 * Returns the singleton instance of the Invoker object
	 * @return 
	 */
	public static Invoker getInstance() {
		if (null == invoker) {
			invoker = new Invoker();
		}
		
		return invoker;
	}
	
	public void addCommand(Command command) {
		commands.add(command);
	}
	
	public void execute() {
		for (Command command : commands) {
			command.execute(aggregator);
		}
	}
	
	public Object executeCommand(Command command) {
		return command.execute(aggregator);
	}
}
